'ui';

auto.setWindowFilter(function (window) {
	return window.title == '米赚'
})

// let storage = storages.create('mz')
// let debug = storage.get('debug','false')
// if (!debug) { 
//     exit()
// }


// 脚本运行线程
let workThread = null
// 脚本运行状态
let isRunning = false
// 悬浮窗口
let wd = null

let btn = null

events.on('exit', function () {
	
})

function showFloaty() {
	wd = floaty.rawWindow(
		<vertical w="*" h="*">
			<frame layout_weight="1" h="30" layout_gravity="center">
				<img
					w="*"
					h="30"
					src="#ffffff"
					borderWidth="1dp"
					scaleType="fitXY"
					circle="true"
				/>
				<text
					id="start"
					h="30"
					text="开始运行"
					textSize="12sp"
					gravity="center"
					padding="15 0 "
				/>
			</frame>
			<frame h="30" layout_weight="1" layout_gravity="center" marginTop="2">
				<img
					w="*"
					h="30"
					src="#ffffff"
					borderWidth="1dp"
					scaleType="fitXY"
					circle="true"
				/>
				<text
					id="stop"
					h="30"
					text="结束运行"
					textSize="12sp"
					gravity="center"
					padding="15 0"
				/>
			</frame>
		</vertical>
	)
	wd.setPosition(20, device.height / 2)
	wd.exitOnClose()
	wd.stop.on('click', function () {
		isRunning = false
		if (workThread) {
			workThread.interrupt()
			workThread = null
		}
		exit()
		toastLog('运行结束')
	})
	wd.start.on('click', function () {
		//程序开始运行之前判断无障碍服务
		if (!auto.service) {
			toast('请先开启无障碍服务！')
			app.startActivity({
				action: 'android.settings.ACCESSIBILITY_SETTINGS',
			})
			return
		}

		if (isRunning) {
			toastLog('运行暂停')
			isRunning = false
			ui.run(function () {
				wd.start.setText('开始运行')
			})
			// threads.shutDownAll()
			return
		}
		
		isRunning = true
		ui.run(function () {
			wd.start.setText('暂停运行')
		})

		if (!workThread) {
			workThread = threads.start(function () {
				toastLog('运行开始')
				app.launchApp('米赚')
				while (true) {
					try {
						while (!isRunning) { }
						main()
					} catch (err) {
						console.log(err)
						// exit()
					}
				}
			})
		}
	})
}

threads.start(function () {
	// 	// // 检测是否打开无障碍权限
	// 	// if (!floaty.checkPermission()) {
	// 	// 	floaty.requestPermission()
	// 	// 	exit()
	// 	// }
	// 	// sleep(1000)
	showFloaty()
})


ui.statusBarColor('#40a5f3')
ui.layout(
	<vertical>
		<appbar bg="#40a5f3">
			<toolbar id="title" title="米赚辅助" />
		</appbar>
		<Switch
			id="floatx"
			text="悬浮窗权限"
			checked="{{floaty.checkPermission()}}"
			padding="8 8 8 8"
			textSize="15sp"
			gravity="right"
		/>
		<Switch
			id="autoService"
			text="无障碍服务"
			checked="{{auto.service != null}}"
			padding="8 8 8 8"
			textSize="15sp"
			gravity="right"
		/>
	</vertical>
)



ui.floatx.on('click', function (checked) {
	floaty.requestPermission()
})
ui.autoService.on('check', function (checked) {
	// 用户勾选无障碍服务的选项时，跳转到页面让用户去开启
	if (checked && auto.service == null) {
		app.startActivity({
			action: 'android.settings.ACCESSIBILITY_SETTINGS',
		})
	}
	if (!checked && auto.service != null) {
		auto.service.disableSelf()
	}
})

// 当用户回到本界面时，resume事件会被触发
ui.emitter.on('resume', function () {
	// 此时根据无障碍服务的开启情况，同步开关的状态
	ui.floatx.checked = floaty.checkPermission()
	ui.autoService.checked = auto.service != null
})

ui.emitter.on('back_pressed', function (event) {
	if (workThread && workThread.isAlive()) {
		event.consumed = true
	}
})

function main() {
	if (!text('我的订单').exists()) return
    btn = className('android.widget.Button').textStartsWith('抢单').clickable().findOne(1000)
    if (btn) { 
        console.log(btn);
        btn.click()
        sleep(1000)
    }
}




